package com.example.photo_sharing.page.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.photo_sharing.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ImgEditAdapter extends RecyclerView.Adapter<ImgEditAdapter.findViewHolder> {

    private OnItemClickListener onItemClickListener;
    private Context context;
    private ArrayList<String> goodsEntities;

    public ImgEditAdapter(Context context, ArrayList<String> goodsEntities){
        System.out.println("asdasdasdadsasdasd--------------"+goodsEntities.size());

        this.context = context;
        this.goodsEntities = goodsEntities;

    }

    @Override
    public ImgEditAdapter.findViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.list_item_img_edit,null);
        return new findViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ImgEditAdapter.findViewHolder holder, @SuppressLint("RecyclerView") int position) {
        String data = goodsEntities.get(position);
        System.out.println();
        Picasso.get().load(data).into(holder.list_image);
        //删除草稿的事件
        holder.mItemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.OnDeleteClick(view,position,data);
                notifyItemRemoved(position);
                System.out.println(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodsEntities.size();
    }

    class findViewHolder extends RecyclerView.ViewHolder {

        //定义控件
        private ImageView list_image;
        private ImageView mItemButton;

        public findViewHolder( View itemView) {
            super(itemView);
            //找到控件
            list_image = itemView.findViewById(R.id.edit_list_img);
            mItemButton = itemView.findViewById(R.id.edit_list_canal);
            System.out.println("asdasdasdadsasdasd--------------");
            //设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (onItemClickListener!=null){

                        onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));
                    }
                }
            });

        }
    }

    //设置点击事件监听器
    public interface OnItemClickListener {
        void OnItemClick(View view, String data);
        void OnDeleteClick(View view,int position, String data);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

